﻿using System;
using System.IO;
using System.Windows.Forms;
using Editors;
using TheWorldLibrary;

namespace The_World_Editors
{
    public partial class FormMain : Form
    {
        #region Field Region

        private static string _gamePath = "";
        private static string _classPath = "";
        private static string _itemPath = "";
        private static string _chestPath = "";
        private static string _keyPath = "";
        private static string _skillPath = "";
        private static string _mapPath = "";
        private FormArmor _frmArmor;
        private FormChest _frmChest;
        private FormClasses _frmClasses;
        private FormKey _frmKey;
        private FormMapEditor _frmMapEditor;
        private FormShield _frmShield;
        private FormSkill _frmSkill;
        private FormWeapon _frmWeapon;
        private RolePlayingGame _rolePlayingGame;

        //private UpdateApp _update;

        #endregion

        #region Property Region

        public static string GamePath
        {
            get { return _gamePath; }
        }

        public static string ClassPath
        {
            get { return _classPath; }
        }

        public static string ItemPath
        {
            get { return _itemPath; }
        }

        public static string ChestPath
        {
            get { return _chestPath; }
        }

        public static string KeyPath
        {
            get { return _keyPath; }
        }

        public static string SkillPath
        {
            get { return _skillPath; }
        }

        public static string MapPath
        {
            get { return _mapPath; }
        }

        #endregion

        #region Constructor Region

        public FormMain()
        {
            InitializeComponent();

            FormClosing += FormMain_FormClosing;

            newGameToolStripMenuItem.Click += newGameToolStripMenuItem_Click;
            openGameToolStripMenuItem.Click += openGameToolStripMenuItem_Click;
            saveGameToolStripMenuItem.Click += saveGameToolStripMenuItem_Click;
            exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;

            classesToolStripMenuItem.Click += classesToolStripMenuItem_Click;
            armorToolStripMenuItem.Click += armorToolStripMenuItem_Click;
            shieldToolStripMenuItem.Click += shieldToolStripMenuItem_Click;
            weaponToolStripMenuItem.Click += weaponToolStripMenuItem_Click;

            keysToolStripMenuItem.Click += keysToolStripMenuItem_Click;
            chestsToolStripMenuItem.Click += chestsToolStripMenuItem_Click;

            skillsToolStripMenuItem.Click += skillsToolStripMenuItem_Click;

            mapEditorToolStripMenuItem.Click += mapEditorToolStripMenuItem_Click;

            //_update = new UpdateApp(this);
        }

        #endregion

        #region Menu Item Event Handler Region

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show(
                "Unsaved changes will be lost. Are you sure you want to exit?",
                "Exit?",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (result == DialogResult.No)
                e.Cancel = true;
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (var frmNewGame = new FormNewGame())
            {
                DialogResult result = frmNewGame.ShowDialog();

                if (result == DialogResult.OK && frmNewGame.RolePlayingGame != null)
                {
                    var folderDialog = new FolderBrowserDialog();

                    folderDialog.Description = "Select folder to create game in.";
                    folderDialog.SelectedPath = Application.StartupPath;

                    DialogResult folderResult = folderDialog.ShowDialog();

                    if (folderResult == DialogResult.OK)
                    {
                        try
                        {
                            _gamePath = Path.Combine(folderDialog.SelectedPath, "Game");
                            _classPath = Path.Combine(_gamePath, "Classes");
                            _itemPath = Path.Combine(_gamePath, "Items");
                            _keyPath = Path.Combine(_gamePath, "Keys");
                            _chestPath = Path.Combine(_gamePath, "Chests");
                            _skillPath = Path.Combine(_gamePath, "Skills");
                            _mapPath = Path.Combine(_gamePath, "Maps");

                            if (Directory.Exists(_gamePath))
                                throw new Exception("Selected directory already exists.");

                            Directory.CreateDirectory(_gamePath);
                            Directory.CreateDirectory(_classPath);
                            Directory.CreateDirectory(_itemPath + @"\Armor");
                            Directory.CreateDirectory(_itemPath + @"\Shield");
                            Directory.CreateDirectory(_itemPath + @"\Weapon");
                            Directory.CreateDirectory(_keyPath);
                            Directory.CreateDirectory(_chestPath);
                            Directory.CreateDirectory(_skillPath);
                            Directory.CreateDirectory(_mapPath);

                            _rolePlayingGame = frmNewGame.RolePlayingGame;
                            XnaSerializer.Serialize(_gamePath + @"\Game.xml", _rolePlayingGame);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                            return;
                        }

                        classesToolStripMenuItem.Enabled = true;
                        itemsToolStripMenuItem.Enabled = true;
                        keysToolStripMenuItem.Enabled = true;
                        chestsToolStripMenuItem.Enabled = true;
                        skillsToolStripMenuItem.Enabled = true;
                        mapEditorToolStripMenuItem.Enabled = true;
                    }
                }
            }
        }

        private void openGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();

            folderDialog.Description = "Select Game folder";
            folderDialog.SelectedPath = Application.StartupPath;

            bool tryAgain = false;

            do
            {
                DialogResult folderResult = folderDialog.ShowDialog();
                DialogResult msgBoxResult;

                if (folderResult == DialogResult.OK)
                {
                    if (File.Exists(folderDialog.SelectedPath + @"\Game.xml"))
                    {
                        try
                        {
                            OpenGame(folderDialog.SelectedPath);
                            tryAgain = false;
                        }
                        catch (Exception ex)
                        {
                            msgBoxResult = MessageBox.Show(
                                ex.ToString(),
                                "Error opening game.",
                                MessageBoxButtons.RetryCancel);

                            if (msgBoxResult == DialogResult.Cancel)
                                tryAgain = false;
                            else if (msgBoxResult == DialogResult.Retry)
                                tryAgain = true;
                        }
                    }
                    else
                    {
                        msgBoxResult = MessageBox.Show(
                            "Game not found, try again?",
                            "Game does not exist",
                            MessageBoxButtons.RetryCancel);

                        if (msgBoxResult == DialogResult.Cancel)
                            tryAgain = false;
                        else if (msgBoxResult == DialogResult.Retry)
                            tryAgain = true;
                    }
                }
            } while (tryAgain);
        }

        private void saveGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_rolePlayingGame != null)
            {
                try
                {
                    XnaSerializer.Serialize(_gamePath + @"\Game.xml", _rolePlayingGame);
                    FormDetails.WriteEntityData();
                    FormDetails.WriteItemData();
                    FormDetails.WriteChestData();
                    FormDetails.WriteKeyData();
                    FormDetails.WriteSkillData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error saving game.");
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void classesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmClasses == null)
            {
                _frmClasses = new FormClasses();
                _frmClasses.MdiParent = this;
            }

            _frmClasses.Show();
            _frmClasses.BringToFront();
        }

        private void armorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmArmor == null)
            {
                _frmArmor = new FormArmor();
                _frmArmor.MdiParent = this;
            }

            _frmArmor.Show();
            _frmArmor.BringToFront();
        }

        private void shieldToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmShield == null)
            {
                _frmShield = new FormShield();
                _frmShield.MdiParent = this;
            }

            _frmShield.Show();
            _frmShield.BringToFront();
        }

        private void weaponToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmWeapon == null)
            {
                _frmWeapon = new FormWeapon();
                _frmWeapon.MdiParent = this;
            }

            _frmWeapon.Show();
            _frmWeapon.BringToFront();
        }

        private void chestsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmChest == null)
            {
                _frmChest = new FormChest();
                _frmChest.MdiParent = this;
            }

            _frmChest.Show();
            _frmChest.BringToFront();
        }

        private void keysToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmKey == null)
            {
                _frmKey = new FormKey();
                _frmKey.MdiParent = this;
            }

            _frmKey.Show();
            _frmKey.BringToFront();
        }

        private void skillsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmSkill == null)
            {
                _frmSkill = new FormSkill();
                _frmSkill.MdiParent = this;
            }

            _frmSkill.Show();
            _frmSkill.BringToFront();
        }

        private void mapEditorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_frmMapEditor == null)
            {
                _frmMapEditor = new FormMapEditor();
                _frmMapEditor.MdiParent = this;
            }

            _frmMapEditor.Show();
            _frmMapEditor.BringToFront();
        }

        #endregion

        #region Method Region

        private void OpenGame(string path)
        {
            _gamePath = path;
            _classPath = Path.Combine(_gamePath, "Classes");
            _itemPath = Path.Combine(_gamePath, "Items");
            _keyPath = Path.Combine(_gamePath, "Keys");
            _chestPath = Path.Combine(_gamePath, "Chests");
            _skillPath = Path.Combine(_gamePath, "Skills");
            _mapPath = Path.Combine(_gamePath, "Maps");

            if (!Directory.Exists(_keyPath))
            {
                Directory.CreateDirectory(_keyPath);
            }

            if (!Directory.Exists(_chestPath))
            {
                Directory.CreateDirectory(_chestPath);
            }

            if (!Directory.Exists(_skillPath))
            {
                Directory.CreateDirectory(_skillPath);
            }

            if (!Directory.Exists(_mapPath))
            {
                Directory.CreateDirectory(_mapPath);
            }

            _rolePlayingGame = XnaSerializer.Deserialize<RolePlayingGame>(
                path + @"\Game.xml");

            FormDetails.ReadEntityData();
            FormDetails.ReadItemData();
            FormDetails.ReadKeyData();
            FormDetails.ReadChestData();
            FormDetails.ReadSkillData();

            PrepareForms();
        }

        private void PrepareForms()
        {
            if (_frmClasses == null)
            {
                _frmClasses = new FormClasses();
                _frmClasses.MdiParent = this;
            }

            _frmClasses.FillListBox();

            if (_frmArmor == null)
            {
                _frmArmor = new FormArmor();
                _frmArmor.MdiParent = this;
            }

            _frmArmor.FillListBox();

            if (_frmShield == null)
            {
                _frmShield = new FormShield();
                _frmShield.MdiParent = this;
            }

            _frmShield.FillListBox();

            if (_frmWeapon == null)
            {
                _frmWeapon = new FormWeapon();
                _frmWeapon.MdiParent = this;
            }

            _frmWeapon.FillListBox();

            if (_frmKey == null)
            {
                _frmKey = new FormKey();
                _frmKey.MdiParent = this;
            }

            _frmKey.FillListBox();

            if (_frmChest == null)
            {
                _frmChest = new FormChest();
                _frmChest.MdiParent = this;
            }

            _frmChest.FillListBox();

            if (_frmSkill == null)
            {
                _frmSkill = new FormSkill();
                _frmSkill.MdiParent = this;
            }

            if (_frmMapEditor == null)
            {
                _frmMapEditor = new FormMapEditor();
                _frmMapEditor.MdiParent = this;
            }

            _frmSkill.FillListBox();

            classesToolStripMenuItem.Enabled = true;
            itemsToolStripMenuItem.Enabled = true;
            keysToolStripMenuItem.Enabled = true;
            chestsToolStripMenuItem.Enabled = true;
            skillsToolStripMenuItem.Enabled = true;
            mapEditorToolStripMenuItem.Enabled = true;
        }

        #endregion
    }
}